<?php
declare (strict_types = 1);

namespace app\subscribe;

use app\common\GptService;
use app\common\QuestionService;
use app\model\QuestionAnswer;
use app\model\QuestionAnswerComment;
use app\model\Users;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Log;

class Question
{

    protected string $eventPrefix = 'Question';

    /**
     * 回复增加点赞
     * @param $id
     * @return true
     */
    function onAnswerIncLike($id)
    {
        $model = new QuestionAnswer();
        $model->where('id',$id)->inc('like')->update();
        return true;
    }
    

    /**
     * 回复减少点赞
     * @param $id
     * @return true
     */
    function onAnswerDecLike($id)
    {
        $model = new QuestionAnswer();
        $model->where('id',$id)->dec('like')->update();
        return true;
    }

    /**
     * 回复增加收藏
     * @param $id
     * @return true
     */
    function onAnswerIncCollect($id): bool
    {
        $model = new QuestionAnswer();
        $model->where('id',$id)->inc('collect')->update();
        return true;
    }

    /**
     * 回复减少收藏
     * @param $id
     * @return true
     */
    function onAnswerDecCollect($id): bool
    {
        $model = new QuestionAnswer();
        $model->where('id',$id)->dec('collect')->update();
        return true;
    }

    /**
     * 增加浏览人数
     * @param $id
     * @return bool
     */
    function onIncBrowse($id): bool
    {
        $model = new \app\model\Question();
        $model->where('id',$id)->inc('browse')->update();
        return true;
    }

    /**
     * 增加互动
     * @param $id
     * @return true
     */
    function onIncComment($id)
    {
        $model = new \app\model\Question();
        $model->where('id',$id)->inc('comment')->update();
        return true;
    }
	
	/**
	 * 增加回复数量
	 * @param $id
	 * @return true
	 */
	function onIncAnswer($id)
	{
		$model = new \app\model\Question();
		$model->where('id',$id)->inc('answer')->update();
		return true;
	}
    /**
     * 回复-增加评论
     * @param $id
     * @return true
     */
    function onAnswerIncComment($id)
    {
        $model = new QuestionAnswer();
        $model->where('id',$id)->inc('comment')->update();
        return true;
    }

    /**
     * 机器人回复
     * @param $id
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    function onBot($id)
    {
     
        $info = \app\model\Question::find($id);
        $bot=Users::where(['bot'=>1])->column('id');
        $k=array_rand($bot);
        $uid=$bot[$k];
        $res=GptService::ask($info->title);
        $res=json_decode($res);
        if ($res->code!=200){
            return false;
        }
        $data=[
            'users_id'=>$uid,
            'question_id'=>$id,
            'content'=>$res->data->choices[0]->message->content,
            'type'=>0,
            'bot'=>1
        ];
        QuestionService::answer($data);
        return true;
    }

    /**
     * 评论增加点赞
     * @param $id
     * @return true
     */
    function onAnswerCommentIncLike($id)
    {
        $model = new QuestionAnswerComment();
        $model->where('id',$id)->inc('like')->update();
        return true;
    }


    /**
     * 评论减少点赞
     * @param $id
     * @return true
     */
    function onAnswerCommentDecLike($id)
    {
        $model = new QuestionAnswerComment();
        $model->where('id',$id)->dec('like')->update();
        return true;
    }
}
